//     https://ac.nowcoder.com/acm/contest/11215/A


#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        int score = 0;
        int m = 0;
        cin >> n >> k;
        while (n--)
        {
            char ans;
            cin >> ans;
            if (ans == 'W')
            {
                m += 1;
                if (m < 3)
                    score += 1;
                else if (m >= 3)
                    score += k;
            }
            else if (ans == 'L')
            {
                m = 0;
                score -= 1;
            }
        }
        cout << score << endl;
    }
    return 0;
}